package com.winning.sercurity.service.Impl;

import com.winning.sercurity.dao.TzAlgorithmEntityMapper;
import com.winning.sercurity.dto.AlgorithmSaveDto;
import com.winning.sercurity.entity.TzAlgorithmEntity;
import com.winning.sercurity.service.AlgorithmManageService;
import com.winning.sercurity.vo.AlgorithmParamQueryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @classname:AlgorithmManageServiceImpl
 * @description: 算法管理实现类.
 * @author: suboyang
 * @create: 2022/10/18-10:17
 **/
@Service
public class AlgorithmManageServiceImpl implements AlgorithmManageService {

    @Autowired
    private TzAlgorithmEntityMapper tzAlgoritymEntityMapper;

    @Override
    public boolean updateParam(AlgorithmSaveDto algorithmSaveDto) {
        TzAlgorithmEntity tzAlgorithmEntity = new TzAlgorithmEntity();
        tzAlgorithmEntity.setSfdm(algorithmSaveDto.getAlgorithmId());
        tzAlgorithmEntity.setGxsj(new Date());
        tzAlgorithmEntity.setSfcs(algorithmSaveDto.getAlgorithmCode());
        int updateResult = tzAlgoritymEntityMapper.updateByPrimaryKeySelective(tzAlgorithmEntity);
        return updateResult != 0;
    }

    @Override
    public List<AlgorithmParamQueryVo> queryParam() {
        List<TzAlgorithmEntity> tzAlgorithmEntityList = tzAlgoritymEntityMapper.selectAll();
        if (CollectionUtils.isEmpty(tzAlgorithmEntityList)) {
            return new ArrayList<>(0);
        }
        List<AlgorithmParamQueryVo> algorithmParamQueryVos = tzAlgorithmEntityList.stream().map(TzAlgorithmEntity ->{
            AlgorithmParamQueryVo algorithmParamQueryVo = new AlgorithmParamQueryVo();
            algorithmParamQueryVo.setAlgorithmId(TzAlgorithmEntity.getSfdm());
            algorithmParamQueryVo.setAlgorithmCode(TzAlgorithmEntity.getSfmc());
            algorithmParamQueryVo.setValue(TzAlgorithmEntity.getSfcs());
            return algorithmParamQueryVo;
        }).collect(Collectors.toList());
        return algorithmParamQueryVos;
    }
}
